let app = getApp();
var siteinfo = require('../../siteinfo.js');
Page({
  data: {
    status: '课程介绍',
    chapterList:[],
    list: [],
    no_data: false, //“没有数据”的变量，默认false，隐藏
    reachLoading: true, //"上拉加载"的变量，默认false，隐藏
    current: 1, //第几页,第几次加载
    number: 16, //需要返回数据的个数
    selectedFlag:false,
    hiddenName:false
  },

  onLoad: function (options) {
    this.setData({
      id: options.id
    })
    this.getCourseDetails()
    this.getChapterList()
   
  },
  onShow: function () {
    this.getCoustuQueAns()
  },
  //获取详情数据
  getCourseDetails: function () {
    var _this = this
    app._get(siteinfo.selCourseDetails, {
      id: _this.data.id
    }, function (result) {
      _this.setData(result.data);
    })
  },
  //获取章节列表
  getChapterList: function () {
    var _this = this
    app._get(siteinfo.selChapterList, {
      id: _this.data.id
    }, function (result) {
      var chapterList = result.data
      chapterList.forEach(function(item,index){
        var resourceList = item.resources;
        if(resourceList!=''){
          resourceList.forEach(function(item2,index2){
          })
      }
      _this.setData({
        chapterList:result.data,
        resourceList:resourceList
      });
      })
      
    })
  },
  //章节详情
  play:function(e){
    // let _this = this
    // let str = JSON.stringify(_this.data.chapterList);
    // console.log(str)
    wx.navigateTo({
     url: '/pages/videoPlay/videoPlay?chapterId=' + e.currentTarget.dataset.id + '&courseId=' + this.data.id+'&resourceId='+e.currentTarget.dataset.id,
    // url: '/pages/videoPlay/videoPlay?str=' + str,
    })

  },
  open: function (e) {
    var key = e.currentTarget.dataset.key;
    var val=e.currentTarget.dataset.value;
    key = key + '.isOpen';
    this.setData({
      [key]: !val
    });
  },
   //获取问答列表
   getCoustuQueAns: function () {
    var _this = this
    app._get(siteinfo.selCoustuQueAns, {
      courseId: _this.data.id,
      number: _this.data.number,
      current: _this.data.current
    }, function (result) {
      _this.setData({
        list: result.data.records
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.stopPullDownRefresh();
    })
  },
  
  onPullDownRefresh: function () {
    this.getCoustuQueAns()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.searchScrollLower();
  },
  searchScrollLower: function () {
    let _this = this;
    if (_this.data.reachLoading && !_this.data.no_data) {
      _this.setData({
        current: _this.data.current + 1, //每次触发上拉事件，把current+1
      });
      wx.showLoading({
        title: '加载中',
      })
      //网络请求
      _this.reachAppendMsgList();
    }
  },
  /**
   * 上拉追加消息列表
   */
  reachAppendMsgList: function () {
    var _this = this;
    app._getLogin(siteinfo.selCoustuQueAns, {
      courseId: _this.data.id,
      number: _this.data.number,
      current: _this.data.current
    }, function (result) {
      //以/n把字符串进行拆分
      var list = result.data.records;
      _this.setData({
        list: _this.data.list.concat(result.data.records)
      });
      if ((result.data.current * result.data.size) >= result.data.total) {
        _this.setData({
          no_data: true
        });
      }
      wx.hideLoading();
    });
  },
  /**
   * 返回顶部
   */
  goTop: function (e) { // 一键回到顶部
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },

  /**
   * 显示/隐藏 返回顶部按钮
   */
  onPageScroll: function (e) {
    this.setData({
      floorstatus: e.scrollTop > 200
    })
  },
  /**
    * 切换标签
    */
  bindHeaderTap: function (e) {
    this.setData({
      status: e.target.dataset.status
    });
  },

//报名课程
  toPay:function(){
    var _this = this
    if (_this.data.type==1){
      app._getLogin(siteinfo.selUserDetail, {}, function (result) {
        if (result.data.state !=1){ //审核未通过
          wx.showModal({
            title: '友情提示',
            content: '个人信息审核通过才能报名该课程',
            showCancel:false
          })
        }else{
          wx.navigateTo({
            url: '/pages/studyPay/studyPay?id=' + _this.data.id,
          })
        }
      })
    }else{
      wx.navigateTo({
        url: '/pages/studyPay/studyPay?id=' + _this.data.id,
      })
    }
  
  },

  changeToggle:function(e){
    var index = e.currentTarget.dataset.index;
    if (this.data.selectedFlag[index]){
      this.data.selectedFlag[index] = false;
    }else{
      this.data.selectedFlag[index] = true;
    }

    this.setData({
      selectedFlag: this.data.selectedFlag
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})